@echo off
echo.
echo.
@echo %DATE% %TIME%
rem ***************************************************************************
rem Builds the release configuration of Wyvern, including installs and everything
rem ***************************************************************************
SET NOSMOKE=0

::the command line switch copy allows the script to be restarted at the copy portion of the build
::this addresses problems where we run out of space on midas to hold the new build....without rebuilding
if /i "%1"=="copy" SET NOSMOKE=1&&goto :copy
if /i "%1"=="smoke" goto :copy



rem ***************************************************************************
rem Comment out the following line if you do not want email notification
SET AUTOMATED_BUILD=True

rem ***************************************************************************
rem CPR7 vs. CPR9
rem
rem DFR - 1/8/08
SET CPR_NUMBER=%1
rem DFR - 05/27/08 - Remove all personalization build stuff - now done automatically...
if "%CPR_NUMBER%"=="7" goto SETUP_7
if "%CPR_NUMBER%"=="9" goto SETUP_9

rem
echo.
echo.
echo Wyvern_DoCompleteReleaseBuild.bat - ERROR: BAD OR MISSING PARAMETER - %1
echo ------------------------------------------------------------------------
echo. 
echo.
echo Build will terminate !
exit /b 1
rem
:SETUP_7
echo **************************
echo There are outstanding issues to resolve before a CPR7 build can complete
echo Need to make sure that trace diagnostics is up-to-date and if using a CPR9 PIA.DLL for it
echo make sure there is a publisher policy to allow direction to the older version
echo
echo the CPR# on the splash screen and the help about need to be updated
echo please see \\rsi-midas\product_bus\Business Development\Prerelease\Wyvern\VersionNumberChanges\AssetCentreVersionNumber.txt
echo
echo************************
goto DONE_ERROR

rem DFR - 07/17/08 - added CPR_STRING_FOR_INSTALL_DISPLAY
rem DFR - 08/18/08 - added CPR_STRING_FOR_3RD_PARTY_DEPEND

SET CPR_STRING=CPR7
SET CPR_STRING_FOR_INSTALL_DISPLAY="CPR7"
SET CPR_STRING_FOR_3RD_PARTY_DEPEND="CPR7"
SET CPR_NUMBER=7
SET RB_VERSION_ONLY=2.30.00
goto SETUP_DONE
rem
:SETUP_9
SET CPR_STRING=CPR9
SET CPR_STRING_FOR_INSTALL_DISPLAY="CPR9 SR2"
REM ATTENTION when upgrading the next line to CPR9SR2 be aware that activation was upgraded earlier by hard coding the path (wyvern_buildInstalls.bat)
SET CPR_STRING_FOR_3RD_PARTY_DEPEND="CPR9SR2"
SET CPR_NUMBER=9
SET RB_VERSION_ONLY=3.00.00
goto SETUP_DONE
rem
:SETUP_DONE
echo.
rem DFR - 1/8/08
echo Wyvern_DoCompleteReleaseBuild.bat - - Building %RB_VERSION_ONLY% %CPR_STRING%
echo.
echo.
rem ***************************************************************************
rem Configuration build server path of Wyvern, please modify it according to your location 
call SetBuildServerPath.bat

rem uncomment out following line for test release build
rem SET TEST_RELEASE=True
rem ***************************************************************************

if "%TEST_RELEASE%"=="True" goto noconfigspec

goto noconfigspec

rem Build Config Spec for "NOW"
rem SET CONFIG_SPEC=AssetCentreBuild.cs
rem echo Creating and Applying Config Spec for Build (%CONFIG_SPEC%)
rem echo >%CONFIG_SPEC% element * CHECKEDOUT
rem DateTimeCSFormat >>%CONFIG_SPEC%
rem echo >>%CONFIG_SPEC% element * /main/LATEST
rem echo >>%CONFIG_SPEC% load \RSMACC_Development
rem cleartool setcs %CONFIG_SPEC%

:noconfigspec

rem Auto increment release version
echo Auto incrementing release version

@echo Checking out Wyvern_lastver.txt
cleartool co -nc -nwarn Wyvern_lastver.txt
if ERRORLEVEL 1 goto DONE_ERROR

rem DFR - 1/8/08 - changed this util to form a complete version number xx.yy.zz.1234
rem by taking a version number (%RB_VERSION_ONLY%), getting the build num and incrementing it, then writing out files as before
rem FIRST, we have to retrieve the build num file
copy "%FTACBuildServer%\BuildNumber\BuildNumber.txt" .
AutoIncrVersion.exe "Wyvern_lastver.txt" %RB_VERSION_ONLY% RB_VERSION
if ERRORLEVEL 1 goto DONE_ERROR
copy BuildNumber.txt "%FTACBuildServer%\BuildNumber\" /Y
rem COPY file back to network

@echo Checking in Wyvern_lastver.txt
cleartool ci -nc -ide Wyvern_lastver.txt
if ERRORLEVEL 1 goto DONE_ERROR

rem this bat file generated from the AutoIncrVersion utility call above
call AutoUpdateVersion.bat
echo Auto incrementing release version - Done

:SetDefaultVersion
rem ***************************************************************************
rem Set release version - if none set, then use the default of 1.00.00.01
if "%RB_VERSION%"=="" SET RB_VERSION=1.00.00.01

rem ***************************************************************************
rem Set log file data
SET LOG_FILE_RB=ReleaseBuild_%RB_VERSION%.Log
if EXIST %LOG_FILE_RB% (del %LOG_FILE_RB%)
if EXIST tempReleaseBuild.txt (del tempReleaseBuild.txt)

rem if "%TEST_RELEASE%"=="True" goto StartCompile
if "%TEST_RELEASE%"=="True" goto UpdateVersions

rem echo Generating CR List for Build %RB_VERSION%
rem ClearQuestQuery username password %RB_VERSION% >CRList_%RB_VERSION%.Log

echo Doing release build %RB_VERSION% >> %LOG_FILE_RB%

rem ***************************************************************************
rem Delete dlls and exes
echo Deleting dlls and exes
echo Deleting dlls and exes >> %LOG_FILE_RB%

pushd ..
call CleanPrep.bat
popd

echo Deleting dlls and exes - done
echo Deleting dlls and exes - done >> %LOG_FILE_RB%
rem ***************************************************************************

:UpdateCC
rem ***************************************************************************
rem Updates the current Clear Case view
rem
rem removed because the cleartool setcs forces an update
rem
echo updating clear case files...
echo updating clear case files >> %LOG_FILE_RB%
cleartool update -nov -force >> %LOG_FILE_RB%
if errorlevel 1 goto DONE_ERROR
echo updating clear case files - Done
echo updating clear case files - Done >> %LOG_FILE_RB%
rem ***************************************************************************

:UpdateVersions
rem ***************************************************************************
echo updating versions
echo updating versions >> %LOG_FILE_RB%
rem DFR - 1/8/08 - 07/17/08
call Wyvern_UpdateVersions.bat %CPR_STRING% %CPR_NUMBER% %RB_VERSION_ONLY% %RB_VERSION% %CPR_STRING_FOR_INSTALL_DISPLAY% >> %LOG_FILE_RB%
if ERRORLEVEL 1 goto DONE_ERROR
echo updating versions - Done
echo updating versions - Done >> %LOG_FILE_RB%
rem ***************************************************************************

:StartCompile

rem ***************************************************************************
echo updating help context ID enumeration
echo updating help context ID enumeration >> %LOG_FILE_RB%
call Wyvern_UpdateHelp.bat >> %LOG_FILE_RB%
if ERRORLEVEL 1 goto DONE_ERROR
echo updating help context ID enumeration - Done
echo updating help context ID enumeration - Done >> %LOG_FILE_RB%
rem ***************************************************************************

rem ***************************************************************************

rem call vsvars32.bat to set deneve.exe path in DevEnvDir variable
call "%VS80COMNTOOLS%vsvars32.bat"

echo Installing RA Key Container >> %LOG_FILE_RB%
rem deletes the RA Public/private keys from the container called RAKeyContainer
sn.exe -q -d RAKeyContainer

rem installs the RA Public/private keys into a container called RAKeyContainer
sn.exe -q -i "\\usmkevfile001\RSI-Midas\ManagedCodeKeys\RAPublicPrivateKeys.snk" RAKeyContainer
if ERRORLEVEL 1 goto DONE_ERROR
echo Installing RA Key Container - Done >> %LOG_FILE_RB%
rem ***************************************************************************

rem ***************************************************************************
echo Compiling solutions >> %LOG_FILE_RB%
SET LOG_FILE_COMPILE=CompileLog_%RB_VERSION%.log
rem DFR - 1/8/08 - LAST
rem DFR - 05/27/08 - Remove all personalization build stuff - now done automatically...
call Wyvern_CompileSolutions.bat Release %CPR_STRING%
if %COMPILE_ERRORLEVEL% NEQ 0 goto DONE_ERROR
echo Solutions compiled. Log file %LOG_FILE_COMPILE%  >> %LOG_FILE_RB%
rem ***************************************************************************

rem ***************************************************************************
echo Doing NUnitTest >> %LOG_FILE_RB%
NUnitTestForFTAC.exe /Proj="..\NUnitFTACTest.nunit"
if %ERRORLEVEL% NEQ 0 goto NUNIT_FAILED
echo Doing NUnit Test - Done >> %LOG_FILE_RB%
rem ***************************************************************************

rem ***************************************************************************
echo Signing outputs >> %LOG_FILE_RB%
call Find_n_sign.bat
if %COMPILE_ERRORLEVEL% NEQ 0 goto DONE_ERROR
rem ***************************************************************************

rem ***************************************************************************
echo Uninstalling RA Key Container
echo Uninstalling RA Key Container >> %LOG_FILE_RB%
sn.exe -q -d RAKeyContainer
echo Uninstalling RA Key Container - Done
echo Uninstalling RA Key Container - Done >> %LOG_FILE_RB%
rem ***************************************************************************

:MergeDbScripts
rem ***************************************************************************
echo Merging Database scripts
echo Merging Database scripts >> %LOG_FILE_RB%

echo Checking out DbInstallerConfig.xml
echo Checking out DbInstallerConfig.xml >> %LOG_FILE_RB%

cleartool co -nc -nwarn "..\Wyvern\Server\DatabaseScripts\DbInstallerConfig.xml"
if ERRORLEVEL 1 goto DONE_ERROR

"..\Wyvern\Server\DatabaseScripts\DbScriptBuilder.exe" /s /v=%RB_VERSION% "/c=AssetCentreNew.txt"
if errorlevel 1 goto DONE_ERROR

"..\Wyvern\Server\DatabaseScripts\DbScriptBuilder.exe" /s /v=%RB_VERSION% "/c=AssetCentreUpdate.txt"
if errorlevel 1 goto DONE_ERROR

echo Checking in DbInstallerConfig.xml
echo Checking in DbInstallerConfig.xml >> %LOG_FILE_RB%

cleartool ci -nc -ide "..\Wyvern\Server\DatabaseScripts\DbInstallerConfig.xml"
if ERRORLEVEL 1 goto DONE_ERROR

echo Merging Database scripts - Done
echo Merging Database scripts - Done >> %LOG_FILE_RB%
rem ***************************************************************************

:BuildInstalls
rem ***************************************************************************
rem Build installs
echo Building installs
echo Building installs >> %LOG_FILE_RB%
rem DFR - 1/8/08
echo.
call Wyvern_BuildInstalls.bat %CPR_STRING% %CPR_NUMBER% %RB_VERSION_ONLY% %RB_VERSION% %CPR_STRING_FOR_3RD_PARTY_DEPEND% >> %LOG_FILE_RB%
if %INSTALLBUILD_ERRORLEVEL% NEQ 0 goto DONE_ERROR
echo Building installs - Done
echo Building installs - Done >> %LOG_FILE_RB%
rem ***************************************************************************

if "%TEST_RELEASE%"=="True" goto exit_ok

rem grab the symbols for the build....
echo collecting symbols and posting to symbol server
call pdb_to_symbol_server
rem for now don't deal with errors returned from the call

:LabelBuild
rem ***************************************************************************
echo Labelling ClearCase with label BUILD_%RB_VERSION%
echo Labelling ClearCase with label BUILD_%RB_VERSION% >> %LOG_FILE_RB%

rem create label in \RSMACC_Development
cleartool mklbtype -global -comment "Release %RB_VERSION% sources" "BUILD_%RB_VERSION%" >> %LOG_FILE_RB%
rem cleartool mklbtype -replace -global -comment "Release %RB_VERSION% sources" "BUILD_%RB_VERSION%" >> %LOG_FILE_RB%
rem We are ignoring the error here because we can get error
rem if the label already exists. If there was a problem
rem with creating the label type, next statement will fail.
rem if ERRORLEVEL 1 goto DONE_ERROR
echo Labelling RSMACC_Development ClearCase vob
echo Labelling RSMACC_Development ClearCase vob >> %LOG_FILE_RB%
cleartool mklabel -replace -recurse -follow -comment "Auto labelled by build script" "BUILD_%RB_VERSION%" "..\..\RSMACC_Development" >> %LOG_FILE_RB%
rem if ERRORLEVEL 1 goto DONE_ERROR

rem LGS 07/02/2008 - adding lines to label ID vob

rem change vob to \FTAssetCentre_InfoDev
cd ..\..\FTAssetCentre_InfoDev
rem create same label in \FTAssetCentre_InfoDev
cleartool mklbtype -global -comment "Release %RB_VERSION% sources" "BUILD_%RB_VERSION%" >> %LOG_FILE_RB%
rem apply label
echo Labelling FTAssetCentre_InfoDev ClearCase vob
echo Labelling FTAssetCentre_InfoDev ClearCase vob >> %LOG_FILE_RB%
cleartool mklabel -replace -recurse -follow -comment "Auto labelled by build script" "BUILD_%RB_VERSION%" "..\FTAssetCentre_InfoDev" >> %LOG_FILE_RB%
rem change back to orignal vob
cd ..\RSMACC_Development\build
echo Labelling ClearCase - Done >> %LOG_FILE_RB%
rem ***************************************************************************
rem echo Resetting standard config spec
rem cleartool setcs AssetCentreNormalConfigSpec.cs

:copy
rem ********************************************************************
if not defined CPR_NUMBER goto env_not_setup
rem Copy files to network (versioned folder)
echo Copying files to network (Versioned folder) >> %LOG_FILE_RB%

SET SRC_DIR="C:\InstallOutput\FinalImage"
SET DEST_DIR="%FTACBuildServer%\%RB_VERSION%-%CPR_STRING%\"
xcopy %SRC_DIR% %DEST_DIR% /Y /R /E /I >> %LOG_FILE_RB%
if errorlevel 1 goto DONE_ERROR

echo Copying files to network (Versioned folder) - done >> %LOG_FILE_RB%

SET WYVERN_BASE="%FTACBuildServer%"
SET AUTOTEST_LOCATION=%WYVERN_BASE%\AutomatedTestTriggers

rem copy the list of folks who should get the smoke test success email....
xcopy success.lst %AUTOTEST_LOCATION% /Y /R /E /I
rem copy the list of folks who should get the smoke test FAIL email...
xcopy builder.lst %AUTOTEST_LOCATION% /Y /R /E /I
rem Use CPR_STRING and RB_VERSION_ONLY to create/update automated test trigger
rem delete the old file first then create
if "%NOSMOKE%"==1 goto :NOSMOKE
if EXIST %AUTOTEST_LOCATION%\%RB_VERSION_ONLY%-%CPR_STRING%.TXT (del %AUTOTEST_LOCATION%\%RB_VERSION_ONLY%-%CPR_STRING%.TXT)
echo %WYVERN_BASE%\%RB_VERSION%-%CPR_STRING% >%AUTOTEST_LOCATION%\%RB_VERSION_ONLY%-%CPR_STRING%.TXT
:NOSMOKE

rem ********************************************************************

rem - DFR - 09/23/08 - no more latest folder

rem ********************************************************************
rem DFR - 9/23/08
rem Copy files to network (DVD folder) - Always do for now...
echo Copying files to network (DVD folder)
echo Copying files to network (DVD folder) >> %LOG_FILE_RB%

SET SRC_DIR="C:\InstallOutput\FinalImage"
SET DEST_DIR="%FTACBuildServer%\DVD30000\%CPR_STRING%\"

xcopy %SRC_DIR% %DEST_DIR% /Y /R /E /I >> %LOG_FILE_RB%
if errorlevel 1 goto DONE_ERROR
goto DONE_DVD_COPY

:DONE_DVD_COPY
echo Copying files to network (DVD folder) - done >> %LOG_FILE_RB%
rem ********************************************************************

goto exit_ok

:DONE_ERROR
echo ----------------------------------- 
echo ERROR! ERROR! ERROR! ERROR! ERROR!
echo Error building release %RB_VERSION%
echo -----------------------------------	
echo ----------------------------------- 	>> %LOG_FILE_RB%
echo ERROR! ERROR! ERROR! ERROR! ERROR!		>> %LOG_FILE_RB%
echo Error building	release %RB_VERSION%	>> %LOG_FILE_RB%
echo -----------------------------------	>> %LOG_FILE_RB%

if "%AUTOMATED_BUILD%"=="True" goto SendEmailFail
if EXIST %LOG_FILE_RB% start notepad.exe %LOG_FILE_RB%
if EXIST CompileLog_%RB_VERSION%.log start notepad.exe CompileLog_%RB_VERSION%.log
exit /b 1

:env_not_setup
echo ENVIRONMENT not setup, can only use copy switch from a window that has been used to build
exit /b 1

:exit_ok
echo Build %RB_VERSION% completed successfully >> %LOG_FILE_RB%
echo Build %RB_VERSION% completed successfully

if "%AUTOMATED_BUILD%"=="True" goto SendEmailSuccess

if EXIST %LOG_FILE_RB% start notepad.exe %LOG_FILE_RB%
if EXIST CompileLog_%RB_VERSION%.log start notepad.exe CompileLog_%RB_VERSION%.log
pause
exit /B

:SendEmailFail
SET BUILD_STATUS=Failed
del notice.txt
echo AssetCentre build %RB_VERSION% has been failed: >> notice.txt
echo %LOG_FILE_RB% and CompileLog_%RB_VERSION%.log are attached. >> notice.txt
echo .
echo .
echo If you received this email in error or wish to be removed from this email list send a request to: >> notice.txt
echo ccai@ra.rockwell.com >> notice.txt
for /F %%i in (builder.lst) do call BuildSendMail.bat %%i notice.txt
exit /b

:SendEmailSuccess
SET BUILD_STATUS=Succeeded
SET BUILD_POST_LOCATION=%FTACBuildServer%\%RB_VERSION%-%CPR_STRING%\
del notice.txt
echo AssetCentre build %RB_VERSION% has been posted to the following locations: >> notice.txt
echo %BUILD_POST_LOCATION% >> notice.txt
echo %LOG_FILE_RB% and CompileLog_%RB_VERSION%.log are attached. >> notice.txt
echo .
echo .
if %NOSMOKE%==1 echo ATTENTION The smoke test for this build was skipped.
echo .
echo If you received this email in error or wish to be removed from this email list send a request to: >> notice.txt
echo ccai@ra.rockwell.com >> notice.txt
for /F %%i in (success.lst) do call BuildSendMail.bat %%i notice.txt
if EXIST %LOG_FILE_RB% start notepad.exe %LOG_FILE_RB%
if EXIST CompileLog_%RB_VERSION%.log start notepad.exe CompileLog_%RB_VERSION%.log
ECHO DO NOT CLOSE THIS BUILD WINDOW UNTIL THE SMOKE TEST HAS PASSED!
pause
exit /B

:NUNIT_FAILED
echo ----------------------------------- 
echo ERROR! ERROR! ERROR! ERROR! ERROR!
echo Error doing nunit test
echo -----------------------------------
exit /b 1
